package cool.houge.pig.repository.user;

import cool.houge.pig.model.User;
import org.springframework.data.r2dbc.repository.Modifying;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import reactor.core.publisher.Mono;

/**
 * 用户数据访问仓库.
 *
 * @author KK (kzou227@qq.com)
 */
public interface UserRepository extends ReactiveCrudRepository<User, Long> {

  /**
   * @param id
   * @param ver
   * @param newStatus
   * @return
   */
  @Query(
      "UPDATE t_user SET status=:newStatus,update_time=CURRENT_TIMESTAMP() WHERE id=:id AND ver=:ver")
  @Modifying
  Mono<Void> updateStatus(long id, int ver, int newStatus);

  /**
   * @param username
   * @return
   */
  @Query("SELECT * FROM t_user WHERE email=:username")
  Mono<User> findByUsername(String username);

  /**
   * 使用指定的微信 UnionId 查询用户信息.
   *
   * @param weixinUnionId 微信 UnionId
   * @return
   */
  @Query("SELECT * FROM t_user WHERE weixin_union_id=:weixinUnionId")
  Mono<User> findByWeixinUnionId(String weixinUnionId);
}
